using UnityEngine;
using QFramework;
using QFramework.Example;
using System.Collections.Generic;
using JetBrains.Annotations;
using Unity.Mathematics;

namespace ProjectSurvivor
{

	public partial class EnemyGenerator : ViewController
	{
		[SerializeField]
		public LevelConfig Config;
		private float mCurrentGenerateSeconds = 0;//当前生成秒数
		private float mCurrentWaveSeconds = 0;  //当前波次描述
		public static BindableProperty<int> EnemyCount = new BindableProperty<int>(0);//

		private Queue<EnemyWave> mEnemyWavesQueue = new Queue<EnemyWave>();//敌人波次队列
		public int WaveCount = 0;//波次
		private int mTotalCount = 0;
		public bool LastWave => WaveCount == mTotalCount;//
		public EnemyWave CurrentWave => mCurrentWave;//
		void Start()
		{
			foreach (var group in Config.EnemyWaveGroups)
			{
				foreach (var wave in group.Waves)
				{
					mEnemyWavesQueue.Enqueue(wave);
					mTotalCount++;
				}
			}
			// Code Here

		}
		private EnemyWave mCurrentWave = null;

		private void Update()
		{
			//当前波次为空
			if (mCurrentWave == null)
			{
				//队列大于0
				if (mEnemyWavesQueue.Count > 0)
				{
					//波次++；
					WaveCount++;
					//当前波次=出列
					mCurrentWave = mEnemyWavesQueue.Dequeue();
					//当前生成秒数
					mCurrentGenerateSeconds = 0;
					//当前波次秒数
					mCurrentWaveSeconds = 0;
				}
			}
			//现在波次不为空
			if (mCurrentWave != null)
			{
				mCurrentGenerateSeconds += Time.deltaTime;
				mCurrentWaveSeconds += Time.deltaTime;
				//达到生成秒数
				if (mCurrentGenerateSeconds >= mCurrentWave.GenerateDuration)
				{
					mCurrentGenerateSeconds = 0;
					//玩家不为空
					var player = Player.Default;
					if (player)
					{
						var xOry = RandomUtility.Choose(-1, 1);
						var pos = Vector2.zero;
						if (xOry == -1)
						{
							pos.x = RandomUtility.Choose(CameraController.LBTrans.position.x,
							CameraController.LBTrans.position.x);
							pos.y = UnityEngine.Random.Range(CameraController.LBTrans.position.y,
							CameraController.RTTrans.position.y);

						}
						else
						{
							pos.x = RandomUtility.Choose(CameraController.LBTrans.position.x,
			CameraController.LBTrans.position.x);
							pos.y = RandomUtility.Choose(CameraController.LBTrans.position.y,
							CameraController.RTTrans.position.y);

						}
						// //随机角度
						// var randomAngle = UnityEngine.Random.Range(0, 360f);
						// //半径                        将角度（度）转换为弧度（radians）弧长/半径=弧度
						// var randomRadius = randomAngle * Mathf.Deg2Rad;
						// //方向
						// var direction = new Vector3(Mathf.Cos(randomRadius), Mathf.Sin(randomRadius));
						// //位置
						// var generatePos = player.transform.position + direction * 10;
						//敌人创建显示
						mCurrentWave.EnemyPrefab
						.Instantiate()
						// .Position(generatePos)
						.Position(pos)
						.Self(self =>
						{
							var enemy = self.GetComponent<IEnemy>();
							enemy.SetSpeedScale(mCurrentWave.SpeedScale);
							enemy.SetHPScale(mCurrentWave.HPScale);
						})
						.Show();
					}
				}
				//当前波次秒数达到
				if (mCurrentWaveSeconds >= mCurrentWave.Seconds)
				{
					//清空，准备下一波
					mCurrentWave = null;
				}
			}
		}
	}
}
